package com.turing.post.capitalaccounts.impl;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.SaleCreditDepositBill;
import com.turing.post.capitalaccounts.util.SaleCreditDepositBillDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "capitalaccounts.impl.SaleCreditDepositBillDaoImpl")
@Remote
@Local
public class SaleCreditDepositBillDaoImpl implements SaleCreditDepositBillDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public SaleCreditDepositBill getSaleCreditDepositBillById(BigInteger id) {
		return _em.find(SaleCreditDepositBill.class, id);
	}

	@Override
	public void insertSaleCreditDepositBill(SaleCreditDepositBill obj)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, obj);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCreditDepositBill> getSaleCreditDepositBill(PCPSWebInput webInput,
			Date startTranDate, Date endTranDate, String saleBranchNo,
			BigInteger startAmount, BigInteger endAmount, String depositType,
			String voucherNo,String isSingle,Integer page) {
		String issueCompanyNo="";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			 issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		}
		/* 日期范围(tranDate)、机构范围(SaleBranchNo外键)、金额范围(amount)、缴款类型范围(DepositType) */
		String sql = "select t.* from SaleCreditDepositBill t where IssueCompanyNo='"+issueCompanyNo+"'";
		if (null != startTranDate)// 起始日期不为空
			sql += " and t.trandate >= to_date('"
					+ new SimpleDateFormat("yyyy-MM-dd").format(startTranDate)
					+ " 00:00:00','yyyy-MM-dd hh24:mi:ss')";// to_date('2012/12/17','yyyy/MM/dd')";
		if (null != endTranDate) {// 终止日期不为空
			sql += " and t.trandate <= to_date('"
					+ new SimpleDateFormat("yyyy-MM-dd").format(endTranDate)
					+ " 23:59:59','yyyy-MM-dd hh24:mi:ss')";
		}
		if (!PCPSUtil.isEmpty(isSingle) && isSingle.equals("1")){
			if (!PCPSUtil.isEmpty(saleBranchNo))
				sql += " and t.salebranchno = '" + saleBranchNo+ "'";
		}else{
			if (!PCPSUtil.isEmpty(saleBranchNo))
				sql += " and t.salebranchno like rtrim('" + saleBranchNo
						+ "','0')||'%'";
		}
		if (null != startAmount)
			sql += " and t.amount >=" + startAmount;
		if (null != endAmount)
			sql += " and t.amount <=" + endAmount;
		if (!PCPSUtil.isEmpty(depositType))
			sql += " and t.deposittype='" + depositType + "'";
		if (!PCPSUtil.isEmpty(voucherNo))
			sql += " and t.voucherNo='" + voucherNo + "'";
		sql+=" order by tranTime";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, SaleCreditDepositBill.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getSaleCreditDepositBillCount(PCPSWebInput webInput,Date startTranDate,
			Date endTranDate, String saleBranchNo, BigInteger startAmount,
			BigInteger endAmount, String depositType,String voucherNo,String isSingle) {
		String issueCompanyNo="";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			 issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		}
		/* 日期范围(tranDate)、机构范围(SaleBranchNo外键)、金额范围(amount)、缴款类型范围(DepositType) */
		String sql = "select nvl(count(*),0) from SaleCreditDepositBill t where IssueCompanyNo='"+issueCompanyNo+"'";
		if (null != startTranDate)// 起始日期不为空
			sql += " and t.trandate >= to_date('"
					+ new SimpleDateFormat("yyyy-MM-dd").format(startTranDate)
					+ " 00:00:00','yyyy-MM-dd hh24:mi:ss')";// to_date('2012/12/17','yyyy/MM/dd')";
		if (null != endTranDate) {// 终止日期不为空
			sql += " and t.trandate <= to_date('"
					+ new SimpleDateFormat("yyyy-MM-dd").format(endTranDate)
					+ " 23:59:59','yyyy-MM-dd hh24:mi:ss')";
		}
		if (!PCPSUtil.isEmpty(isSingle) && isSingle.equals("1")){
			if (!PCPSUtil.isEmpty(saleBranchNo))
				sql += " and t.salebranchno = '" + saleBranchNo+ "'";
		}else{
			if (!PCPSUtil.isEmpty(saleBranchNo))
				sql += " and t.salebranchno like rtrim('" + saleBranchNo
						+ "','0')||'%'";
		}
		if (null != startAmount)
			sql += " and t.amount >=" + startAmount;
		if (null != endAmount)
			sql += " and t.amount <=" + endAmount;
		if (!PCPSUtil.isEmpty(depositType))
			sql += " and t.deposittype='" + depositType + "'";
		if (!PCPSUtil.isEmpty(voucherNo))
			sql += " and t.voucherNo='" + voucherNo + "'";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void updateSaleCreditDepositBill(SaleCreditDepositBill obj)
			throws PCPSApplicationException {
		GeneralDao.update(_em, obj);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SaleCreditDepositBill getSaleCreditDepositBillByCancelId(
			BigInteger cancelId) {
		String sql = "select * from salecreditdepositbill t where t.cancelsalecreditdepositbillid="
				+ cancelId;
		Query query = _em.createNativeQuery(sql, SaleCreditDepositBill.class);
		List<SaleCreditDepositBill> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public SaleCreditDepositBill getSaleCreditDepositBillByIdForUpdate(
			BigInteger id) {
		Query query=_em.createNativeQuery("select t.* from salecreditdepositbill t " +
				"where t.salecreditdepositbillid= ?1 for update",
				SaleCreditDepositBill.class);
		query.setParameter(1, id);
		return (SaleCreditDepositBill) query.getSingleResult();
	}

}
